﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public class RedisMQ
    {
        public static void Produce(string queue)
        {
            if (queue == null)
                return;
            try
            {
                new RedisHelper().ListLeftPush(CacheKey.PUBLIC_REDIS_MQ_CACHE, queue);
            }
            catch (Exception e)
            {
                LoggerHelper.WriteLog("RedisMQ.Produce", string.Format("json:【{0}】，message:【{1}】", JsonConvert.SerializeObject(queue), e.Message));
            }
        }

        /// <summary>
        /// MQ生产
        /// </summary>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static void Produce<T>(RedisQueue<T> queue)
        {
            if (queue == null)
                return;
            try
            {
                new RedisHelper().ListLeftPush(CacheKey.PUBLIC_REDIS_MQ_CACHE, queue);
            }
            catch (Exception e)
            {
                LoggerHelper.WriteLog("RedisMQ.Produce", string.Format("json:【{0}】，message:【{1}】",JsonConvert.SerializeObject(queue), e.Message));
            }
        }

        /// <summary>
        /// MQ消费
        /// </summary>
        /// <returns></returns>
        public static string Consume()
        {
            try
            {
                return new RedisHelper().ListRightPop(CacheKey.PUBLIC_REDIS_MQ_CACHE);
            }
            catch (Exception e)
            {
                LoggerHelper.WriteErrorLog("RedisMQ.Consume", e);
                return null;
            }
        }

        /// <summary>
        /// MQ消费
        /// </summary>
        /// <returns></returns>
        public static RedisQueue<T> Consume<T>()
        {
            try
            {
                return new RedisHelper().ListRightPop<RedisQueue<T>>(CacheKey.PUBLIC_REDIS_MQ_CACHE);
            }
            catch (Exception e)
            {
                LoggerHelper.WriteErrorLog("RedisMQ.Consume", e);
                return null;
            }
        }
    }
}
